package org.scboy.dbms.controller;

import java.util.List;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.DictionaryTypeJoinService;
import org.scboy.dbms.service.DictionaryTypeService;
import org.scboy.dbms.service.DictionaryTypeValueService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("base/dictionarytype/")
@Menu("微服务管理系统.字典类型管理")
public class DictionaryTypeController {
	
	@Autowired
	DictionaryTypeService typeService;
	
	@Autowired
	DictionaryTypeJoinService typeJoinService;

	@Autowired
	DictionaryTypeValueService typeValueService;
	
	@PostMapping("page")
	@Permission("分页查询字典类型")
	public Result page(@RequestBody Model params){
		Page page=typeService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	@GetMapping("list")
	@Permission("查询字典类型集合")
	public Result list(Model params){
		List<Model> list=typeService.list(params);
		return Result.Success(list,0L);	
	}
	
	/**
	 * 新增文件字典
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增字典类型")
	public Result add(@RequestBody Model params){
		if(typeService.add(params)){
			return Result.Success("BASE_DICTIONARYTYPE_ADD_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYTYPE_ADD_ERROR");
		}
	}
	
	/**
	 * 获取字典
	 * @param request
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取字典类型详情")
	public Result get(String typeCode){
		return Result.Success(typeService.get(typeCode),"");
	}
	
	/**
	 * 更新字典
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新字典类型")
	public Result update(@RequestBody Model params){
		if(typeService.update(params)){
			return Result.Success("BASE_DICTIONARYTYPE_UPDATE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYTYPE_UPDATE_ERROR");
		}		
	}

	/**
	 * 删除字典
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除字典类型")
	public Result delete(String typeCode){
		if(typeService.delete(typeCode)){
			return Result.Success("BASE_DICTIONARYTYPE_DELETE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYTYPE_DELETE_ERROR");
		}
	}
	
	/**
	 * 启用禁用字典
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用字典类型")
	public Result checkState(String typeCode){
		if(typeService.checkState(typeCode)){
			return Result.Success("BASE_DICTIONARYTYPE_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("BASE_DICTIONARYTYPE_CHECKSTATE_ERROR");
		}
	}

	/**
	 * 分页查询字典类型和字典关系列表
	 * @param request
	 * @return
	 */
	@PostMapping("dtjpage")
	@Permission("分页查询字典类型和字典关系列表")
	public Result dtjPage(@RequestBody Model params) {
		Page page = typeJoinService.page(params);
		return Result.Success(page.getData(), page.getCount());
	}
	/**
	 * 分页查询字典类型和字典关系列表
	 * @param request
	 * @return
	 */
	@PostMapping("dtjlist")
	@Permission("查询字典类型和字典关系列表")
	public Result dtjList(@RequestBody Model params) {
		return Result.Success(typeJoinService.list(params),"");
	}
	
	/**
	 * 新增字典类型和字典关系
	 * @param request
	 * @param userTypeId
	 * @param roleId
	 * @return
	 */
	@PostMapping("adddtj")
	@Permission("新增字典类型和字典关系")
	public Result adddtj(String typeCode,String[] code) {
		if (typeJoinService.add(typeCode, code)) {
			return Result.Success("RBAC_DICTIONARY_DICTIONARYTYPE_ADD_SUCCESS");
		} else {
			return Result.Error("RBAC_DICTIONARY_DICTIONARYTYPE_ADD_ERROR");
		}
	}

	/**
	 * 删除字典类型和字典关系
	 * @param request
	 * @param id
	 * @return
	 */
	@DeleteMapping("deletedtj")
	@Permission("删除字典类型和字典关系")
	public Result deletedtj(String id) {
		if (typeJoinService.delete(id)) {
			return Result.Success("RBAC_DICTIONARY_DICTIONARYTYPE_DELETE_SUCCESS");
		} else {
			return Result.Error("RBAC_DICTIONARY_DICTIONARYTYPE_DELETE_ERROR");
		}
	}
	
	/**
	 * 获取字典类型和字典关系
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("getdtj")
	@Permission("获取字典类型和字典关系")
	public Result getdtj(String typeCode) {
		Model model=Model.create().add("code", typeJoinService.getCodes(typeCode)).add("typeCode", typeCode);
		return Result.Success(model,"");
	}
	
	@PostMapping("values")
	@Permission("获取字典类型相关的字典配置")
	public Result values(@RequestBody Model params) {
		return Result.Success(typeValueService.list(params));
	}

	@PostMapping("valuespage")
	@Permission("分页获取字典类型相关的字典配置")
	public Result valuePage(@RequestBody Model params) {
		return Result.Success(typeValueService.page(params));
	}

	@PutMapping("addvalue")
	@Permission("设置字典类型相关的字典值")
	public Result addvalue(@RequestBody Model params) {
		 String typeCode=params.getString("typeCode");
		 String infoId=params.getString("infoId");
		 String value=params.getString("value");
		if(typeValueService.add(typeCode, infoId, value)){
			return Result.Success("RBAC_DICTIONARY_DICTIONARYTYPE_ADDVALUE_SUCCESS");
		}else{
			return Result.Error("RBAC_DICTIONARY_DICTIONARYTYPE_ADDVALUE_ERROR");
		}
	}
	
	@PutMapping("initvalue")
	@Permission("初始化字典类型相关的字典值")
	public Result initTypeValue(String typeCode,String code) {
		if(typeValueService.initTypeValue(typeCode, code)){
			return Result.Success("RBAC_DICTIONARY_DICTIONARYTYPE_INITVALUE_SUCCESS");
		}else{
			return Result.Error("RBAC_DICTIONARY_DICTIONARYTYPE_INITVALUE_ERROR");
		}
	}
	
	@GetMapping("getValue")
	@Permission("获取转换配置值")
	public Result getValue(String typeCode,String infoId) {
		return Result.Success(typeValueService.get(typeCode, infoId));
	}
	
	@DeleteMapping("clearvalue")
	@Permission("清空字典类型相关的字典值")
	public Result clearTypeValue(String typeCode,String code) {
		if(typeValueService.clearTypeValue(typeCode, code)){
			return Result.Success("RBAC_DICTIONARY_DICTIONARYTYPE_CLEARVALUE_SUCCESS");
		}else{
			return Result.Error("RBAC_DICTIONARY_DICTIONARYTYPE_CLEARVALUE_ERROR");
		}
	}
	
}
